package cn.com.smart.module.system.org.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.smart.common.utils.StringUtils;
import cn.com.smart.framework.aspectj.lang.annotation.Log;
import cn.com.smart.framework.aspectj.lang.enums.BusinessType;
import cn.com.smart.framework.web.controller.BaseController;
import cn.com.smart.framework.web.domain.AjaxResult;
import cn.com.smart.framework.web.domain.Ztree;
import cn.com.smart.module.system.org.domain.Org;
import cn.com.smart.module.system.org.service.IOrgService;
import cn.com.smart.module.system.role.domain.Role;

/**
 * 机构信息
 * 
 */
@Controller
@RequestMapping("/system/org")
public class OrgController extends BaseController
{
    private String prefix = "system/org";

    @Autowired
    private IOrgService orgService;

    @RequiresPermissions("system:org:view")
    @GetMapping()
    public String org()
    {
        return prefix + "/org";
    }

    @RequiresPermissions("system:org:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Org> list(Org org)
    {
        List<Org> orgList = orgService.selectOrgList(org);
        return orgList;
    }

    /**
     * 新增机构
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        mmap.put("org", orgService.selectOrgById(parentId));
        return prefix + "/add";
    }

    /**
     * 新增保存机构
     */
    @Log(title = "机构管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:org:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Org org)
    {
        return toAjax(orgService.insertOrg(org));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{orgId}")
    public String edit(@PathVariable("orgId") Long orgId, ModelMap mmap)
    {
        Org org = orgService.selectOrgById(orgId);
        if (StringUtils.isNotNull(org) && 100L == orgId)
        {
            org.setParentName("无");
        }
        mmap.put("org", org);
        return prefix + "/edit";
    }

    /**
     * 保存
     */
    @Log(title = "机构管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:org:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Org org)
    {
        return toAjax(orgService.updateOrg(org));
    }

    /**
     * 删除
     */
    @Log(title = "机构管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:org:remove")
    @GetMapping("/remove/{orgId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("orgId") Long orgId)
    {
        if (orgService.selectOrgCount(orgId) > 0)
        {
            return AjaxResult.warn("存在下级机构,不允许删除");
        }
        if (orgService.checkOrgExistUser(orgId))
        {
            return AjaxResult.warn("机构存在用户,不允许删除");
        }
        return toAjax(orgService.deleteOrgById(orgId));
    }

    /**
     * 校验机构名称
     */
    @PostMapping("/checkorgNameUnique")
    @ResponseBody
    public String checkorgNameUnique(Org org)
    {
        return orgService.checkOrgNameUnique(org);
    }

    /**
     * 选择机构树
     */
    @GetMapping("/selectOrgTree/{orgId}")
    public String selectorgTree(@PathVariable("orgId") Long orgId, ModelMap mmap)
    {
        mmap.put("org", orgService.selectOrgById(orgId));
        return prefix + "/tree";
    }

    /**
     * 加载机构列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = orgService.selectOrgTree(new Org());
        return ztrees;
    }

    /**
     * 加载角色机构（数据权限）列表树
     */
    @GetMapping("/roleOrgTreeData")
    @ResponseBody
    public List<Ztree> orgTreeData(Role role)
    {
        List<Ztree> ztrees = orgService.roleOrgTreeData(role);
        return ztrees;
    }
}
